<?php
/**
 * Jishupu-Laravel-Vue-Admin
 * @Author:技术铺<jishupu@qq.com>
 * @Date: 2021/11/11
 */

namespace App\Http\Controllers;

use App\Filters\NavigationFilter;
use App\Http\Requests\NavigationRequest;
use App\Http\Resources\NavigationCollection;
use App\Http\Resources\NavigationResource;
use App\Models\Navigation;
use App\Services\AttachmentService;
use App\Services\NavigationService;
use App\Services\TreeService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;

/**
 * 导航模块
 * Class NavigationController
 * @package App\Http\Controllers
 */
class NavigationController extends Controller
{

    /**
     * 列表
     * @param NavigationFilter $filter
     * @return \Illuminate\Http\JsonResponse
     */
    public function index(NavigationFilter $filter)
    {
        $filter = Navigation::filter($filter)->get();
        $collection = NavigationCollection::make($filter)->toArray(null);
        $items = TreeService::make($collection['items']);
        return $this->success('', ['items' => $items, 'total' => count($collection['items'])]);
    }

    /**
     * 添加
     * @param NavigationRequest $request
     * @return \Illuminate\Http\JsonResponse
     * @throws \Illuminate\Auth\Access\AuthorizationException
     * @throws \Illuminate\Validation\ValidationException
     */
    public function store(NavigationRequest $request)
    {
        $data = $request->validate('store');
        $navigation = new Navigation($data);
        if (!$navigation->save()) return $this->errorStored();
        return $this->successStored();
    }

    /**
     * 查看
     * @param Navigation $navigation
     * @return \Illuminate\Http\JsonResponse
     */
    public function show(Navigation $navigation)
    {
        $navigation = NavigationResource::make($navigation)->except(['created_at', 'updated_at', 'sort'])->toArray(null);
        return $this->success('', $navigation);
    }

    /**
     * 更新
     * @param NavigationRequest $request
     * @param Navigation $navigation
     * @return \Illuminate\Http\JsonResponse
     * @throws \Illuminate\Auth\Access\AuthorizationException
     * @throws \Illuminate\Validation\ValidationException
     */
    public function update(NavigationRequest $request, Navigation $navigation)
    {
        $data = $request->validate('update');
        if (!$navigation->update($data)) return $this->errorUpdated();
        return $this->successUpdated();
    }

    /**
     * 删除
     * @param Navigation $navigation
     * @return \Illuminate\Http\JsonResponse
     */
    public function destroy(Navigation $navigation)
    {
        DB::beginTransaction();
        try {
            $subNavigation = Navigation::where('parent_id', $navigation->id)->first();
            // 存在下级则禁止删除
            if ($subNavigation) {
                return $this->error(__('navigation.prohibit_delete_with_subs'));
            }
            $navigation->delete();
            DB::commit();
            return $this->successDestoryed();
        } catch (\Exception $e) {
            DB::rollback();
            return $this->exception($e->getMessage())->errorDestoryed();
        }
    }

    /**
     * 更新状态
     * @param NavigationRequest $request
     * @param Navigation $navigation
     * @return \Illuminate\Http\JsonResponse
     * @throws \Illuminate\Auth\Access\AuthorizationException
     * @throws \Illuminate\Validation\ValidationException
     */
    public function updateStatus(NavigationRequest $request, Navigation $navigation)
    {
        $data = $request->validate('updateStatus');
        if (!$navigation->update($data)) return $this->errorUpdated();
        return $this->successUpdated();
    }

    /**
     * 更新排序
     * @param NavigationRequest $request
     * @param Navigation $navigation
     * @return \Illuminate\Http\JsonResponse
     * @throws \Illuminate\Auth\Access\AuthorizationException
     * @throws \Illuminate\Validation\ValidationException
     */
    public function updateSort(NavigationRequest $request, Navigation $navigation)
    {
        $data = $request->validate('updateSort');
        if (!$navigation->batchUpdate($data['sort'])) return $this->errorUpdated();
        return $this->successUpdated();
    }

    /**
     * 上一级选择导航树
     * @param $id // 当前导航id
     * @return \Illuminate\Http\JsonResponse
     */
    public function parentTreeConfig($id)
    {
        return $this->success('', NavigationService::getParentTree($id));
    }
}
